package com.cliff.common.redis;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
/**
 * 描述：Redis 对象序列化实现类<br>
 * 创建者： jack<br>
 * 创建日期：2017年5月11日 下午3:57:10<br>
 * 版本：<br>
 * 修改者：<br>
 * 修改日期：<br>
 */
public class RedisObjectSerializer implements RedisSerializer<Object> {
    private Converter<Object, byte[]> serializer = new SerializingConverter();
    private Converter<byte[], Object> deserializer = new DeserializingConverter();
    static final byte[] EMPTY_ARRAY = new byte[0];
    public Object deserialize(byte[] bytes) {
      if (isEmpty(bytes)) {
        return null;
      }
      try {
        return deserializer.convert(bytes);
      } catch (Exception ex) {
        throw new SerializationException("Cannot deserialize", ex);
      }
    }
    public byte[] serialize(Object object) {
      if (object == null) {
        return EMPTY_ARRAY;
      }
      try {
        return serializer.convert(object);
      } catch (Exception ex) {
        return EMPTY_ARRAY;
      }
    }
    private boolean isEmpty(byte[] data) {
      return (data == null || data.length == 0);
    }
  }